package com.qianfeng.xinyihezi.xinyihezi.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.config.NetConfig;
import com.qianfeng.xinyihezi.xinyihezi.config.RetrofitInterface;
import com.qianfeng.xinyihezi.xinyihezi.model.BuyBean;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class BuyActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView mImageViewBack, mImageViewPack;
    private TextView mTextViewAddress, mTextViewNeed, mTextViewTitle, mTextViewCount, mTextViewMuch, mTextViewMuchCount, mTextViewAdd, mTextViewSub, mTextViewMid, mTextViewFreight, mTextViewPay, mTextViewConfirm;
    private RadioButton mRadioButtonMy, mRadioButtonFrident;
    private RelativeLayout mRelativeLayoutMy, mRelativeLayoutFrident;
    private int count = 1;
    private String id;
    private Retrofit mRetrofit;
    private RetrofitInterface mRetrofitInterface;
    private ProgressDialog mDialog;
    private float price;
    private float freight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy);
        mDialog = new ProgressDialog(this);
        mDialog.setMessage("加载中...");
        mDialog.show();
        initView();
        initData();
        setListener();
    }

    private void setListener() {
        mImageViewBack.setOnClickListener(this);
        mTextViewAdd.setOnClickListener(this);
        mTextViewSub.setOnClickListener(this);
        mRelativeLayoutMy.setOnClickListener(this);
        mRelativeLayoutFrident.setOnClickListener(this);
        mTextViewConfirm.setOnClickListener(this);
        mTextViewAddress.setOnClickListener(this);
    }

    private void setData(BuyBean bean) {
        Picasso.with(this).load(bean.image_url).into(mImageViewPack);
        mTextViewTitle.setText(bean.name);
        mTextViewCount.setText("数量：" + bean.count);
        mTextViewMuch.setText("￥" + bean.p_price);
        mTextViewFreight.setText("￥" + bean.freight);
        freight = Float.parseFloat(bean.freight);
        price = Float.parseFloat(bean.p_price);
        mTextViewPay.setText("￥" + (freight + price));
    }

    private void initData() {
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        mRetrofit = new Retrofit.Builder().baseUrl(NetConfig.BASE_LEFTCLASSIFT).build();
        mRetrofitInterface = mRetrofit.create(RetrofitInterface.class);
        loadData(id);
    }

    private void loadData(String id) {
        mRetrofitInterface.getBuyData(id).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    try {
                        String result = response.body().string().toString();
                        JSONObject jsonObject = new JSONObject(result);
                        BuyBean bean = new BuyBean();
                        JSONObject data = jsonObject.getJSONObject("data");
                        bean.name = data.getString("name");
                        JSONArray products = data.getJSONArray("products");
                        for (int i = 0; i < products.length(); i++) {
                            JSONObject jsonObject1 = products.getJSONObject(i);
                            bean.freight = jsonObject1.getString("freight");
                            bean.image_url = jsonObject1.getString("image_url");
                            bean.p_price = jsonObject1.getString("p_price");
                            JSONObject spec = jsonObject1.getJSONObject("spec");
                            try {
                                bean.count = spec.getString("数量");
                            } catch (JSONException e) {
                                bean.count = spec.getString("包装");
                            }
                        }
                        mDialog.dismiss();
                        setData(bean);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    private void initView() {
        mImageViewBack = (ImageView) findViewById(R.id.iv_buy_back);
        mImageViewPack = (ImageView) findViewById(R.id.iv_buy_pack);
        mTextViewAddress = (TextView) findViewById(R.id.tv_buy_address);
        mTextViewTitle = (TextView) findViewById(R.id.tv_buy_title);
        mTextViewMuch = (TextView) findViewById(R.id.tv_buy_much);
        mTextViewCount = (TextView) findViewById(R.id.tv_buy_count);
        mTextViewMid = (TextView) findViewById(R.id.tv_buy_mid_count);
        mTextViewMuchCount = (TextView) findViewById(R.id.tv_buy_much_count);
        mTextViewAdd = (TextView) findViewById(R.id.tv_buy_add);
        mTextViewSub = (TextView) findViewById(R.id.tv_buy_sub);
        mTextViewFreight = (TextView) findViewById(R.id.tv_buy_freight);
        mTextViewPay = (TextView) findViewById(R.id.tv_buy_pay);
        mTextViewConfirm = (TextView) findViewById(R.id.tv_buy_confirm);
        mTextViewNeed = (TextView) findViewById(R.id.tv_buy_need);
        mRadioButtonMy = (RadioButton) findViewById(R.id.rb_buy_my);
        mRadioButtonFrident = (RadioButton) findViewById(R.id.rb_buy_frident);
        mRelativeLayoutMy = (RelativeLayout) findViewById(R.id.rl_buy_my);
        mRelativeLayoutFrident = (RelativeLayout) findViewById(R.id.rl_buy_frident);
        mRadioButtonMy.setChecked(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_buy_my:
                mRadioButtonFrident.setChecked(false);
                mRadioButtonMy.setChecked(true);
                mTextViewNeed.setText("需支付：");
                if (count == 0) {
                    mTextViewPay.setText("￥" + (price * 1 + freight));
                    return;
                }
                mTextViewPay.setText("￥" + (price * count + freight));
                break;
            case R.id.rl_buy_frident:
                mRadioButtonMy.setChecked(false);
                mRadioButtonFrident.setChecked(true);
                mTextViewNeed.setText("自己需要支付：");
                mTextViewPay.setText("￥" + 0.00);
                break;
            case R.id.iv_buy_back:
                finish();
                break;
            case R.id.tv_buy_add:
                count++;
                mTextViewSub.setEnabled(true);
                mTextViewPay.setText("￥" + (price * count + freight));
                mTextViewMuchCount.setText("x" + count);
                mTextViewMid.setText("" + count);
                break;
            case R.id.tv_buy_sub:
                count--;
                if (count == 0) {
                    Toast.makeText(this, "最小为一哦", Toast.LENGTH_SHORT).show();
                    mTextViewMid.setText("" + 1);
                    mTextViewMuchCount.setText("x" + 1);
                    mTextViewPay.setText("￥" + (price + freight));
                    mTextViewSub.setEnabled(false);
                    return;
                }
                mTextViewPay.setText("￥" + (price * count + freight));
                mTextViewMuchCount.setText("x" + count);
                mTextViewMid.setText("" + count);
                break;
            case R.id.tv_buy_confirm:
                Toast.makeText(this, "支付", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_buy_address:
                Toast.makeText(this, "跳转填写地址信息", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}
